<template>
  <div class="todo-item">
    <div class="container">
      <li >
        <span>{{ content }}</span>
        <span>{{cityTemperature}}℃</span>
      </li>
      <div class="buttons">
        <button @click="handleSearch">查看</button>
        <button @click="handleDelete">删除</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref,onMounted } from 'vue';
const props = defineProps({
  id: {
    type: Number,
  },
  content: {
    type: String,
  },
});

const emit = defineEmits(['del']);

const handleDelete = () => {
  emit('del', props.id);
};

import { useRouter } from 'vue-router';
const router = useRouter();
const handleSearch = () => {
  router.push({
    path: '/weather',
    query: {
      search: props.content,
    },
  });
};

/* 通过接口获取本地天气数据 */
import {getCityAdcodeData, getCityWeatherData} from '@/api/index.js';
const cityAdcodeData = ref({});
const cityWeatherData = ref({});
const cityTemperature = ref('');

onMounted(async () => {
  cityAdcodeData.value = await getCityAdcodeData(props.content);
  cityWeatherData.value = await getCityWeatherData(cityAdcodeData.value.geocodes[0].adcode,'base');
  //console.log(cityWeatherData.value.lives[0].temperature);
  cityTemperature.value = cityWeatherData.value.lives[0].temperature;
});
</script>

<style lang="scss" scoped>
.todo-item {
  color: white;
  &:hover .container>li {
    width: 80%;
  }
  &:hover .container>.buttons {
    display: block;
  }
  .container {
    max-width: 1216px;
    height: 32px;
    line-height: 32px;
    display: flex;
    justify-content: space-between;
    margin: 0 auto;
    margin-bottom: 1rem;
    li {
      width: 100%;
      list-style: none;
      display: flex;
      justify-content: space-between;
      padding: 0 1rem;
      background: #004e71;
    }
    .buttons {
      margin-left: 2rem;
      display: none;
      button {
        width: 80px;
        height: 100%;
        line-height: 100%;
        color: white;
        background-color: rgb(234 179 8);
        border: none;
        margin-left: 10px;
        &:hover {
          cursor: pointer;
          background-color: #ff6700;
        }
      }
    }
  }
}
</style>
